#include "ATM.h"


std::string ATM::getTime()
{
    time_t t;
    time(&t);
    std::string tt=ctime(&t);
    tt.erase(tt.end()-1);       //去掉末尾换行符
    return tt;
}

int ATM::getBalance()
{
    return NowAccount.getBalance();
}

bool ATM::SetAccountID(std::string accountid)
{
    std::ifstream fin;
    std::string filename="./accounts/"+accountid+".txt";
    fin.open(filename.c_str());
    if (!fin.is_open())             //如果打开失败说明没有这个账户
        return false;
    else
    {
        fin>>NowAccount;            //打开成功则将该账户设置为当前操作账户
        fin.close();
        return true;
    }
}

int ATM::IfTrue(std::string password)
{
    if (NowAccount.getPassword()==password)
    {
        int a=NowAccount.PswordTrue();
        save();
        return a;
    }
    else
    {
        int a=NowAccount.PswordWrong();
        save();
        return a;
    }
}

int ATM::withdraw(int money)
{
    if (money%100!=0)
    {
        std::cout<<"You can only withdraw with multiples of 100!";
        return 1;
    }
    else if (money>20000)                       //单笔取款限制20000
        return 2;
    else if (NowAccount.getBalance()<money)     //余额不足
        return -1;

    int AllWithdraw=0;          //当天取款总额
    std::vector<std::string> nowtime=DiviseString5(getTime());          //当前系统时间
    const std::vector<std::pair<int,std::string>>& withdrawrecords=NowAccount.getWithdrawRecords();     //获得取款记录
    for (auto it=withdrawrecords.rbegin();it!=withdrawrecords.rend();it++)      
    {
        std::pair<int,std::string> a=*it;                                       //a: 一条取款记录
        std::vector<std::string> time=DiviseString5(a.second);                  //a的取款时间
        if (time[1]==nowtime[1]&&time[2]==nowtime[2]&&time[4]==nowtime[4])      //比较是否与当前为同一天
            AllWithdraw+=a.first;
        else
            break;
    }
    if (AllWithdraw>=100000)                    //统计当天取款总额是否大于单日取款限制100000
        return 3;

    else                                        //实现取款功能
    {
        NowAccount.SubBalance(money);
        std::string time=getTime();
        NowAccount.AddWithdraw(money,time);
        save();
        return 0;
    }

}

bool ATM::deposit(int money)
{
    NowAccount.AddBalance(money);
    std::string time=getTime();
    NowAccount.AddDeposit(money,time);
    save();
    return true;
}

int ATM::transfer(std::string __account,int money)
{

    if (NowAccount.getBalance()<money)
        return -1;
    else
    {
        std::ifstream fin;
        std::string filename="./accounts/"+__account+".txt";
        fin.open(filename.c_str(),std::ios::in);
        if (!fin.is_open())                         //未打开说明没有这个账户
            return 1;
        else
        {
            account to_account;
            fin>>to_account;
            NowAccount.SubBalance(money);
            to_account.AddBalance(money);
            fin.close();

            std::ofstream fout;
            fout.open(filename,std::ios::out);
            fout<<to_account;
            fout.close();
            save();
            return 0;
        }
    }
}

bool ATM::setPassword(std::string newPassword)
{
    int len=newPassword.size();
    if (len!=6)
        return false;
    for (int i=0;i<6;i++)
        if (newPassword[i]>'9'||newPassword[i]<'0')
            return false;

    NowAccount.SetPassword(newPassword);
    save();
    return true;
}

bool ATM::save()
{
    std::fstream fout;
    std::string filename="./accounts/"+NowAccount.getAccountID()+".txt";
    fout.open(filename);
    fout<<NowAccount;
    fout.close();
    return true;
}

bool ATM::IfLocked()
{
    if (NowAccount.getPswordWrongNum()>=3)
        return true;
    else
        return false;
}

std::vector<std::string> ATM::DiviseString5(std::string a)
{
    std::string after="";
    std::vector<std::string>str;
    for (int i=0;;i++)
    {
        if (str.size()==5)
            return str;
        else if (str.size()==4&&after.size()==4)
        {
            str.push_back(after);
        }
        else if (a[i]!=' '&&a[i]!='\n')
            after+=a[i];
        else
        {
            str.push_back(after);
            after="";
        }
    }
}
